<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.Properties" %>


<portlet:defineObjects />
<%PortletPreferences prefs = renderRequest.getPreferences();
PortletURL actionURL = renderResponse.createActionURL();
%>

<%-- Checking whethere this is the first time --%>
<%
    String url = prefs.getValue("bugzillaUrl", "null");
    if(url.equals("null")){ // this is the first time
        actionURL.setParameter("edit", "link");
%>
        <%-- Give a link to edit --%>
        <a href="<%=actionURL.toString()%>">Set your Bugzilla Login here</a>
<% }else{
      String myBugzillaLogin =(String)renderRequest.getAttribute("BUGZILLA_LOGIN");
      String myBugzillaUrl =(String)renderRequest.getAttribute("BUGZILLA_URL");

%>


    <div class="portlet-msg-info">
        Assigned Issues Description
    </div>

<%
    //Read the Bugzilla application database configuration values from the Administration.properties file.
    URL myURL=application.getResource("/WEB-INF/Administration.properties");
    InputStream in = myURL.openStream();
    Properties p = new Properties();
    p.load( in );

    String myurl = p.getProperty("url");
    String mydbName = p.getProperty("database");
    String mydriver = p.getProperty("driverClassName");
    String myuserName = p.getProperty("username");
    String mypassword = p.getProperty("password");

        //Establish a connection with the Bugzilla application database.
    Connection conn = null;
    Class.forName(mydriver).newInstance();
    conn = DriverManager.getConnection(myurl+mydbName,myuserName,mypassword);

    int myUserIDValue = 0;


    Statement s0 = conn.createStatement ();
    ResultSet resultset0 = s0.executeQuery ("SELECT userid FROM profiles WHERE login_name='" + myBugzillaLogin + "'");
    while (resultset0.next() )
    {
    String myUserID = resultset0.getString("userid");
    myUserIDValue = Integer.parseInt(myUserID);
    //System.out.println(myUserIDValue);
    }


    //--------------------------------Display the issue description of Issues Assigned to the user.---------------------------------
    //Arrays to store the retrieved details of the issues.
    int[] bugIDs = new int[100];
    int[] productIDs = new int[100];
    int[] componentIDs = new int[100];
    String[] issueDecs = new String[100];
    String[] issueSeveritys = new String[100];
    int meme =0;

    //Retrieve the issue details which are assigned to the user and store them in the arrays.
    try {
        Statement s = conn.createStatement ();
        ResultSet resultset = s.executeQuery ("SELECT bug_id, product_id, component_id, short_desc, bug_severity FROM bugs WHERE assigned_to='" + myUserIDValue + "' AND bug_status='NEW' ORDER BY product_id");

        while (resultset.next() )
        {
            String myBugId = resultset.getString("bug_id");
            String myProductId = resultset.getString("product_id");
            String myComponentId = resultset.getString("component_id");
            String myDesc = resultset.getString("short_desc");
            String myBugSeverity = resultset.getString("bug_severity");


            bugIDs[meme] = Integer.parseInt(myBugId);;
            productIDs[meme] = Integer.parseInt(myProductId);;
            componentIDs[meme] = Integer.parseInt(myComponentId);;
            issueDecs[meme] = myDesc;
            issueSeveritys[meme] = myBugSeverity;
            meme = meme+1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

        %>
    <%-- Display the results --%>
    <div class="results-grid">
        <table class="taglib-search-iterator">
                <tr class="portlet-section-header results-header">
                    <th class="col-1 ">Bug ID</th>
                    <th class="col-2">Product</th>
                    <th class="col-3">Component</th>
                    <th class="col-4">Description</th>
                    <th class="col-5">Bug Severity</th>

                </tr>

                <% for(int i = 0; i < meme; i++){%>
                <tr class="portlet-section-body results-row">
                    <td class="col-1"><a href="<% out.println(myBugzillaUrl); %>"><% out.println(bugIDs[i]); %></a></td>
                    <td class="col-2">
                        <%
                            //Display the correct image of the issue type according the retrieved issue type from the database.
                            Statement s1 = conn.createStatement ();
                            ResultSet resultset1 = s1.executeQuery ("SELECT name FROM products WHERE id='" + productIDs[i] + "'");
                            String productName = null;
                            while (resultset1.next() )
                            {
                                productName = resultset1.getString("name");
                                //myUserIDValue = Integer.parseInt(myUserID);
                                //System.out.println(myUserIDValue);
                            }
                            out.println(productName);
                        %>
                    </td>
                    <td class="col-3">
                        <%
                            //Display the correct image of the issue type according the retrieved issue type from the database.
                            Statement s2 = conn.createStatement ();
                            ResultSet resultset2 = s2.executeQuery ("SELECT name FROM components WHERE id='" + componentIDs[i] + "'");
                            String componentName = null;
                            while (resultset2.next() )
                            {
                                componentName = resultset2.getString("name");
                                //myUserIDValue = Integer.parseInt(myUserID);
                                //System.out.println(myUserIDValue);
                            }
                            out.println(componentName);
                        %>
                    </td>
                    <td class="col-4"><% out.println(issueDecs[i]); %></td>
                    <td class="col-5"><% out.println(issueSeveritys[i]); %></td>
                </tr>
                <% } %>
        </table>
    </div>

        <br>
        <div class="portlet-msg-info" align="center">
        Reported Issues Description
        </div>

<%
    //--------------------------------Display the issue description of Issues Reported by the user.---------------------------------
    //Arrays to store the retrieved details of the issues.
    int[] rep_bugIDs = new int[100];
    int[] rep_productIDs = new int[100];
    int[] rep_componentIDs = new int[100];
    String[] rep_issueDecs = new String[100];
    String[] rep_issueSeveritys = new String[100];
    int rep_meme =0;

    //Retrieve the issue details which are Reported by the user and store them in the arrays.
    try {
        Statement s3 = conn.createStatement ();
        ResultSet resultset3 = s3.executeQuery ("SELECT bug_id, product_id, component_id, short_desc, bug_severity FROM bugs WHERE reporter='" + myUserIDValue + "' ORDER BY product_id");

        while (resultset3.next() )
        {
            String rep_myBugId = resultset3.getString("bug_id");
            String rep_myProductId = resultset3.getString("product_id");
            String rep_myComponentId = resultset3.getString("component_id");
            String rep_myDesc = resultset3.getString("short_desc");
            String rep_myBugSeverity = resultset3.getString("bug_severity");

            rep_bugIDs[rep_meme] = Integer.parseInt(rep_myBugId);;
            rep_productIDs[rep_meme] = Integer.parseInt(rep_myProductId);;
            rep_componentIDs[rep_meme] = Integer.parseInt(rep_myComponentId);;
            rep_issueDecs[rep_meme] = rep_myDesc;
            rep_issueSeveritys[rep_meme] = rep_myBugSeverity;
            rep_meme = rep_meme+1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

        %>
    <%-- Display the results --%>
    <div class="results-grid">
        <table class="taglib-search-iterator">
                <tr class="portlet-section-header results-header">
                    <th class="col-1 ">Bug ID</th>
                    <th class="col-2">Product</th>
                    <th class="col-3">Component</th>
                    <th class="col-4">Description</th>
                    <th class="col-5">Bug Severity</th>

                </tr>

                <% for(int i = 0; i < rep_meme; i++){%>
                <tr class="portlet-section-body results-row">
                    <td class="col-1"><a href="<% out.println(myBugzillaUrl); %>"><% out.println(rep_bugIDs[i]); %></a></td>
                    <td class="col-2">
                        <%
                            //Display the correct image of the issue type according the retrieved issue type from the database.
                            Statement s4 = conn.createStatement ();
                            ResultSet resultset4 = s4.executeQuery ("SELECT name FROM products WHERE id='" + rep_productIDs[i] + "'");
                            String rep_productName = null;
                            while (resultset4.next() )
                            {
                                rep_productName = resultset4.getString("name");
                                //myUserIDValue = Integer.parseInt(myUserID);
                                //System.out.println(myUserIDValue);
                            }
                            out.println(rep_productName);
                        %>
                    </td>
                    <td class="col-3">
                        <%
                            //Display the correct image of the issue type according the retrieved issue type from the database.
                            Statement s5 = conn.createStatement ();
                            ResultSet resultset5 = s5.executeQuery ("SELECT name FROM components WHERE id='" + rep_componentIDs[i] + "'");
                            String rep_componentName = null;
                            while (resultset5.next() )
                            {
                                rep_componentName = resultset5.getString("name");
                                //myUserIDValue = Integer.parseInt(myUserID);
                                //System.out.println(myUserIDValue);
                            }
                            out.println(rep_componentName);
                        %>
                    </td>
                    <td class="col-4"><% out.println(rep_issueDecs[i]); %></td>
                    <td class="col-5"><% out.println(rep_issueSeveritys[i]); %></td>
                </tr>
                <% } %>
        </table>
    </div>


  <% } %>

 <%-- Footer to link to Bugzilla --%>
    <hr style="border-bottom: dotted 1px"/>
    <div class="portlet-section-footer" >
        Go To: <a href="<%=url%>" >Bugzilla</a>
    </div>
